<?php

namespace App\Http\Controllers\product;

use App\Http\Controllers\Controller;
use App\Models\product\ProductClassModel;
use App\Models\product\ProductModel;
use App\Models\product\ProductPlatformModel;
use Carbon\Carbon;
use Illuminate\Http\Request;
use lxs\api\ApiResponse;
use lxs\tools\Tools;

class ProductController extends Controller
{

    /**
     * 获取产品广场页面数据
     */
    public function getProductSquareData(Request $request) {

        $type = $request->input('type');
        $isMore = $request->input('is_more');

        $classList = ProductClassModel::orderBy('sort', 'desc')->orderBy('id', 'asc')->get()->toArray();

        if ($type == 'all') {
            $lists = ProductClassModel::orderBy('sort', 'desc')->orderBy('id', 'asc')->get()->toArray();
        } else {
            $lists = ProductClassModel::where('id', $type)->orderBy('sort', 'desc')->orderBy('id', 'asc')->get()->toArray();
        }

        if ($isMore) {
            foreach ($lists as $k => $v) {
                $lists[$k]['product_list'] = ProductModel::where('class_id', $v['id'])
                        ->where('deleted_at', 0)
                        ->orderBy('sort', 'desc')
                        ->orderBy('id', 'desc')
                        ->get()->toArray();
                $lists[$k]['product_list'] = array_chunk($lists[$k]['product_list'], 3);
            }
        } else {
            $lists = array_map(function ($item) {
                $item['product_list'] = ProductModel::where('class_id', $item['id'])
                        ->where('deleted_at', 0)->orderBy('sort', 'desc')
                        ->orderBy('id', 'desc')
                        ->get()->toArray();
                return $item;
            }, $lists);
            $lists = array_filter($lists, function ($item) {
                return count($item['product_list']) > 0;
            });
        }
        return ApiResponse::apiResponseData(200, '获取产品广场页面数据成功', [
            'class_list' => $classList,
            'lists' => $lists,
        ]);

    }


    /**
     * 获取产品详情产品介绍页面数据
     */
    public function getProductDetailIntroduceData(Request $request) {

        $introduce = Tools::Config('introduce');

        return ApiResponse::apiResponseData(200, '获取产品详情产品介绍页面数据成功', [
            'introduce' => $introduce,
        ]);
    }


    /**
     * 获取产品详情页面数据
     */
    public function getProductDetailData(Request $request) {
        $id = $request->input('product_id');

        $product = ProductModel::with(['productPlatform' => function ($query) {
            $query->select('id', 'platform_name');
        }])->where('id', $id)
            ->first();
        if (!$product) {
            return ApiResponse::apiResponseData(400, '产品不存在');
        }

        $platform = ProductPlatformModel::rightJoin('product AS p', 'p.platform_id', '=', 'product_platform.id')
            ->where('p.product_title', $product->product_title)
            ->select('product_platform.platform_cover')
            ->get()
            ->toArray();

        $product_else = ProductModel::with(['productPlatform' => function ($query) {
            $query->select('id', 'platform_name');
        }])->where('product_title', $product->product_title)
            ->where('id', '<>', $id)->get()
            ->toArray();

        $product->live_time = Carbon::createFromTimestamp($product->live_time)->format('Y-m-d');

        return ApiResponse::apiResponseData(200, '获取产品详情页面数据成功', [
            'product' => $product,
            'platform' => $platform,
            'product_else' => $product_else,
        ]);
    }


    /**
     * 获取产品介绍列表页面数据
     */
    public function getProductIntroduceListData(Request $request) {

        $page = $request->input('page');
        $pageSize = $request->input('page_size');

        $model = ProductModel::with(['productPlatform' => function ($query) {
            $query->select('id', 'platform_name');
        }]);

        $count = $model->count();

        $list = $model->orderBy('sort', 'desc')
            ->orderBy('id', 'desc')
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->get()->toArray();

        $list = array_map(function ($item) {
            $item['platform'] = ProductPlatformModel::rightJoin('product AS p', 'p.platform_id', '=', 'product_platform.id')
                ->where('p.product_title', $item['product_title'])
                ->select('product_platform.platform_cover')
                ->get()
                ->toArray();
            return $item;
        }, $list);

        return ApiResponse::success('获取首页图片成功', ['list' => $list, 'total' => $count]);
    }


}
